package com.muki.succ.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 管理员解析器
 * @author ljh_2017
 *
 */
public class ManagerConfiResolver extends DefaultHandler {

	private Map<String,Integer> managermap = new HashMap<>();
	private List<Manager> list;
	private Manager manager;
	private String tagName;  
	
	public List<Manager> getList() {
		return list;
	}
	
	public int getIndex(String name) {
		return managermap.getOrDefault(name, -1);
	}
	
	@Override public void startDocument() throws SAXException {
		list = new ArrayList<>();
		managermap.clear();
	}

	@Override public void endDocument() throws SAXException {
	}

	@Override public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.equals("manager")) {
			manager = new Manager();
		}
		this.tagName = qName;
	}

	@Override  public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("manager")) {
			list.add(manager);
		}
		this.tagName = null;
		
	}

	@Override public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(this.tagName!=null) {
			String data = new String(ch,start,length);
			if(this.tagName.equals("manager-class")) {
				manager.setDriver(data);
			}
			if(this.tagName.equals("bind-topic")) {
				managermap.put(data, list.size());
				manager.setTopic(data);
			}
			if(this.tagName.equals("bind-regx")) {
				manager.setRegx(data);
			}
		}
	}

}
